import salePlanApi from "../../../common/api/breedsales/planbills.api.js"
import { DictionaryType, requestDict } from "../../../common/dictionary/dictionary-type.js";
import { uploadFile, requestFilePath, publicFileDraw, sliceFileName } from "../../../common/api/file-request.js";
import watermark from '../../../common/watermark.js';
import { Collaboration_APP, TEST_NUMBER, YY_NUMBER } from '../../../../../public/js/config/config.js';

(function (window, document, $) {
    $('body').attr('data-weui-theme', 'light'); //强制光明模式

    var _vm = new Vue({
        el: '#vehicle-app',
        data() {
            return {
                form: {},
                pigTypeOptions: []
            }
        },
        created() {
            $(`#vehicle-app`).removeClass('d-none')
            let url = location.href
            let params = {} // url参数
            for (let key of (new URL(url)).searchParams.entries()) {
                key[1] ? params[key[0]] = key[1] : ''
            }
            this.getPigTypeOption()
            this.getDetail(params.id)
            this.watermarkBgImg()
        },
        mounted() {
        },
        methods: {
            // 获取销售品类数组字典
            getPigTypeOption() {
                requestDict(DictionaryType.LEVEL_TYPE).then(res => {
                    this.pigTypeOptions = res.data
                })
            },
            // 根据数据字典值获取名臣
            getNameById(id, options) {
                let obj = options.find(item => item.value == id)
                if (obj) {
                    return obj.displayName
                }
                return id
            },
            // 查询详情
            getDetail(id) {
                salePlanApi.getSalePlanDetail({ id }).then(res => {
                    res.bizDate = moment(res.bizDate).format('YYYY-MM-DD')
                    res.entryDate = moment(res.entryDate).format('YYYY-MM-DD')
                    res.count = Utils.toThousands(res.count, 0)
                    res.pigDays = Utils.toThousands(res.pigDays, 0)
                    res.avgWeight = Utils.toThousands(res.avgWeight)
                    res.difference = `±${Utils.toThousands(res.difference) }`
                    res.attachList = []
                    this.$nextTick(async () => {
                        this.form = res
                        if (Utils.IsNotNull(res.attaches)) {
                            let attachList = res.attaches.split(',')
                            for (let i = 0; i < attachList.length; i++) {
                                await requestFilePath(attachList[i]).then(res => {
                                    this.form.attachList.push(res.data)
                                });
                            }
                        }
                    });
                });
            },
            // 预览图片
            previewImage(item) {
                weui.gallery(item, {
                    className: 'sale-plan-detail'
                });
            },
            // 返回列表
            returnList() {
                location.href = './saleplanlist.html'
            },
            //水印信息
            watermarkBgImg() {
                let _userInfo = JSON.parse(Utils.GetSessionStorage("SNAccountEply")) || {};
                watermark.set(_userInfo.name + _userInfo.numCode);// 水印
                //salePlanApi.queryUserName(Utils.GetQueryString('state') || YY_NUMBER.sourceID).then(res => {
                //    watermark.set(_userInfo.un + res.code);// 水印
                //}).catch(_ => {
                //    watermark.set(_userInfo.un);// 水印
                //});
            }
        }
    });

})(window, document, jQuery)